package com.yl.chainStore.control

import com.yl.chainStore.services.IFileUploadService
import com.yl.common.ErrorCode
import com.yl.common.control.ResponseBody
import com.yl.common.control.returnError
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.multipart.MultipartFile
import javax.servlet.http.HttpServlet
import javax.servlet.http.HttpServletRequest

/**
 * @description :文件上传下载
 * @author :吴枫
 * @create :2021-07-05 15:42:00
 */
@RestController
@RequestMapping("fileUpLoad")
class FileUpLoadController(
        private val fileUploadService: IFileUploadService

) : HttpServlet() {


    /**
     * 上传图片
     */
    @PostMapping("uploadImage")
    fun uploadImage(@RequestBody fileUpload: MultipartFile, req: HttpServletRequest): ResponseBody {
        //检查文件是否为空
        if (fileUpload.isEmpty) {
            return returnError(ErrorCode.PARAMETER_FORMAT_ERROR, "请选择图片")
        }

        return fileUploadService.uploadFile(fileUpload, req)
    }


    /**
     * 上传文件
     */
    @PostMapping("uploadFile")
    fun uploadFile(@RequestBody fileUpload: MultipartFile, req: HttpServletRequest): ResponseBody {
        if (fileUpload.isEmpty) {
            return returnError(ErrorCode.PARAM_EMPTY, "请选择文件")
        }

        return fileUploadService.uploadFile(fileUpload, req)

    }



}

